import java.awt.Canvas;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import com.dinim.graphics.GraphicsContextSwingImpl;
import com.dinim.graphics.IGraphicsContext;
import com.dinim.math.GeometryUtil;
import com.dinim.math.TrigonometryUtil;
import com.dinim.matrix.twodimensional.IPoint2D;
import com.dinim.object.Line2D;
import com.dinim.object.Object2D;
import com.dinim.object.Rectangle2D;
import com.dinim.object.Star2D;
import com.dinim.physics2D.IBoundingBox2D;


public class CanvasStrategyTester {

    static double direction = 1;
    static boolean run = true;
    
    public static void main(String[] args) throws InterruptedException, IOException {
        JFrame f = new JFrame();
        f.setSize(500, 500);
        f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
        
        
        Canvas canvas = new Canvas();
        
        
        f.add(canvas);
        f.setVisible(true);
        
        f.addKeyListener(new KeyListener(){

            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub
                if(e.getKeyText(e.getKeyCode()).equals("Links")){
                    direction = -0.5;
                }
                if(e.getKeyText(e.getKeyCode()).equals("Rechts")){
                    direction = 0.5;
                }
                run = true;
            }

            public void keyReleased(KeyEvent e) {
                run = false;
            }

            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub
                
            }
            
        });
        
        canvas.createBufferStrategy(2);
        BufferStrategy doublebuffer = canvas.getBufferStrategy();
        
        Object2D obj1 = new Star2D(100, 100, 100, 100, 2);
        Object2D obj2 = new Rectangle2D(150, 190, 200, 200);
        
        File imagefile = new File("D:\\Data\\t343490\\Blue hills.jpg");
        
        BufferedImage img = ImageIO.read(imagefile);
        
        
        while(true){
        while(run){
            
            IGraphicsContext gc = new GraphicsContextSwingImpl(doublebuffer.getDrawGraphics());
            gc.setAntialiasing(true);

            //doublebuffer.getDrawGraphics().drawImage(img, 0, 0, canvas.getWidth(), canvas.getHeight(), canvas);
            
            gc.drawFilledRectangle(0, 0, canvas.getWidth(), canvas.getHeight());
            
            obj1.rotate(2*direction, obj1.getCenterPoint());
            //obj1.draw(gc);
            
            obj2.rotate(-1*direction, obj2.getCenterPoint());
            //obj2.draw(gc);
            
            IBoundingBox2D bb = obj1.getBoundingBox();
            
            
            
            
            //gc.drawLine(obj1.getCenterPoint().getX(), obj1.getCenterPoint().getY(), obj2.getCenterPoint().getX(), obj2.getCenterPoint().getY());
            
            Line2D line = new Line2D(obj1.getCenterPoint(), obj2.getCenterPoint());
            
            
            double angle = TrigonometryUtil.getAngleWithAdjacentLeg(line.getPoints()[0].getX() - line.getPoints()[1].getX(), 
                                                     GeometryUtil.getLength(line.getPoints()[0].getX(), 
                                                                            line.getPoints()[0].getY(),
                                                                            line.getPoints()[1].getX(), 
                                                                            line.getPoints()[1].getY()));
            
            line.rotate(angle, line.getCenterPoint());
            
            
            obj1.rotate(angle, line.getCenterPoint());
            obj1.draw(gc);
            obj2.rotate(angle, line.getCenterPoint());
            obj2.draw(gc);
            //line.draw(gc);
            
            
            
            IPoint2D[] points1 = obj1.getPoints();
            IPoint2D[] points2 = obj2.getPoints();
            
            ArrayList p1 = new ArrayList();
            ArrayList p2 = new ArrayList();
            
            for(int i = 0; i < points1.length; i++){
                p1.add(points1[i]);
            }
            for(int i = 0; i < points2.length; i++){
                p2.add(points2[i]);
            }
            
            Comparator comp = new Comparator(){
                public int compare(Object o1, Object o2) {
                    IPoint2D obj1 = (IPoint2D)o1;
                    IPoint2D obj2 = (IPoint2D)o2;
                    double top1 = obj1.getX();
                    double top2 = obj2.getX();
                    int value = (int)Math.round(top2 - top1);
                    return value;
                }
                
            };
            
            
            
            Collections.sort(p1, comp);
            
            
            comp = new Comparator(){

                public int compare(Object o1, Object o2) {
                    IPoint2D obj1 = (IPoint2D)o1;
                    IPoint2D obj2 = (IPoint2D)o2;
                    double top1 = obj1.getX();
                    double top2 = obj2.getX();
                    int value = (int)Math.round(top2 - top1);
                    return value;
                }
                
            };
            
            Collections.sort(p2, comp);
            
            
//            
//            ((ITransformable)obj1).rotate(150, obj1.getPoints()[0]);
//            ((IDrawable)obj1).draw(gc);
//            ((ITransformable)obj1).rotate(-150, obj1.getPoints()[0]);

            IPoint2D obj1p1 = (IPoint2D)p1.get(p1.size()-1);
            IPoint2D obj1p2 = (IPoint2D)p1.get(p1.size()-2);
            
            IPoint2D obj2p1 = (IPoint2D)p2.get(0);
            IPoint2D obj2p2 = (IPoint2D)p2.get(1);
            
            
            gc.drawLine(obj1p1.getX(), obj1p1.getY(), obj2p1.getX(), obj2p1.getY());
            gc.drawLine(obj1p1.getX(), obj1p1.getY(), obj2p2.getX(), obj2p2.getY());
            gc.drawLine(obj1p2.getX(), obj1p2.getY(), obj2p2.getX(), obj2p2.getY());
            gc.drawLine(obj1p2.getX(), obj1p2.getY(), obj2p1.getX(), obj2p1.getY());
            
            Line2D bestLine1 = new Line2D(obj1p1, obj2p1);
            Line2D bestLine2 = new Line2D(obj1p2, obj2p1);
            Line2D bestLine3 = new Line2D(obj2p1, obj1p1);
            Line2D bestLine4 = new Line2D(obj2p2, obj1p1);
            
            
            
            obj1.rotate(-angle, line.getCenterPoint());
            obj2.rotate(-angle, line.getCenterPoint());
            
            doublebuffer.show();

            Thread.sleep(3);
        }
        
    }
    }
    
}
